package csy.view.memeda.testactivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.example.commontools.view.recycleviewadapter.EasyBaseRecycleViewAdapter;
import com.example.commontools.view.recycleviewadapter.EasyRecycleAdapter;
import com.example.commontools.view.recycleviewadapter.EasyRecycleOnScrollListener;
import com.example.commontools.view.refresh.SmartRefreshLayout;
import com.example.commontools.view.refresh.api.RefreshLayout;
import com.example.commontools.view.refresh.listener.OnLoadmoreListener;
import com.example.commontools.view.refresh.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import csy.view.memeda.R;


/**
 * Created by chengsiyuan on 2017/10/14.
 */

public class TestrefreshActivity extends Activity {

    @BindView(R.id.rv)
    RecyclerView mRecyclerView;


    private List<String> datas = new ArrayList<>();

    private RecycleAdapter mRecycleAdapter;

    private View headView;

    private View footView;

    private int page = 1;

    private int pageNum = 20;


    private void initData() {

        for (int i = 0; i < 20; i++) {
            datas.add("item" + i);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testrefresh);
        ButterKnife.bind(this);
        final LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
        SmartRefreshLayout smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshlayout);
        smartRefreshLayout.setEnableLoadmore(false);
        smartRefreshLayout.setEnableRefresh(true);

        initData();
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                Toast.makeText(TestrefreshActivity.this, "refresh", Toast.LENGTH_SHORT).show();
                refreshlayout.finishRefresh(2000);
                datas.clear();
                initData();
                mRecycleAdapter.notifyDataSetChanged();
            }
        });


//        回弹的上拉加载
//        smartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
//            @Override
//            public void onLoadmore(RefreshLayout refreshlayout) {
//                refreshlayout.finishLoadmore();
//                page++;
//                Toast.makeText(TestrefreshActivity.this, "load", Toast.LENGTH_SHORT).show();
//                initData();
//                mRecycleAdapter.notifyDataSetChanged();
//            }
//        });


        assert mRecyclerView != null;
        mRecyclerView.setHasFixedSize(true);

        //设置LayoutManager
        mRecyclerView.setLayoutManager(mLinearLayoutManager);

        //设置分割线
        //mRecyclerView.addItemDecoration(new EasyDividerItemDecoration(this ,EasyDividerItemDecoration.VERTICAL_LIST));

        //创建Adapter
        mRecycleAdapter = new RecycleAdapter(mRecyclerView, datas);

        //使用EasyAdapter包装一层 添加头部 尾部
        EasyRecycleAdapter mEasyRecycleAdapter = new EasyRecycleAdapter(mRecycleAdapter);

        //添加头部
        createHead();
        mEasyRecycleAdapter.addHeaderView(headView);

        //添加尾部 上拉加载更多
        createFoot();
        mEasyRecycleAdapter.addFooterView(footView);

        //设置适配器
        mRecyclerView.setAdapter(mEasyRecycleAdapter);

        //设置item点击事件
        mRecycleAdapter.setOnItemClickListener(new EasyBaseRecycleViewAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(int position, EasyBaseRecycleViewAdapter.ClickableViewHolder holder) {

                Toast.makeText(TestrefreshActivity.this, datas.get(position), Toast.LENGTH_SHORT).show();
            }
        });

        //添加Scroll监听 上拉加载更多数据，不回弹的上拉加载
        mRecyclerView.addOnScrollListener(new EasyRecycleOnScrollListener(mLinearLayoutManager) {

            @Override
            public void onLoadMore(int currentPage) {


                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        //这里是上拉更多数据的逻辑
                        page++;
                        for (int i = 0; i < 20; i++) {
                            datas.add("更多数据" + i);
                        }
                        mRecycleAdapter.notifyDataSetChanged();
                    }
                }, 2000);


            }
        });


    }

    private void createHead() {

        headView = LayoutInflater.from(this).inflate(R.layout.layout_recycle_head, mRecyclerView, false);
    }

    private void createFoot() {

        footView = LayoutInflater.from(this).inflate(R.layout.load_more_foot_layout, mRecyclerView, false);
    }

}
